from fastapi import APIRouter, Depends

from app.api.response import Response
from app.api.status import Status
from app.utils.jwt_service import gen_jwt
from app.middleware.auth import get_current_user
from app.business.user import is_user_exist, get_user_info


login_router = APIRouter()


@login_router.post("/user/login", summary="userLogin")
def login(
        account: str,
        password: str
):
    if not is_user_exist(account):
        return Response.failure(msg="用户不存在", status=Status.UNAUTHORIZED_ERROR)
    data = get_user_info(account)
    return Response.success(data={"token": gen_jwt(data)})


@login_router.get("/user/{user_id}", summary="user详情")
def get(
        current_user: str = Depends(get_current_user),
):
    return Response.success(data=current_user)
